<template>
  <div :ref="ref" :style="{width: '100%', height: '100%' }" />
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'AssetInstitutionChartChart',
  props: {
    data: {
      required: true,
      type: Array,
      default: function() {
        return []
      }
    },
    title: {
      required: true,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ref: 'assetInstitutionChart' + Math.floor(Math.random() * 1000000)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    init() {
      const that = this
      const stChart = echarts.init(this.$refs[this.ref])
      const xAxisData = this.data.map(it => it.name)
      const seriesData = this.data.map(it => it.value)

      const option = {
        title: {
          text: that.title
        },
        xAxis: {
          type: 'category',
          data: xAxisData,
          axisLabel: {
            interval: 0,
            rotate: 30
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: seriesData,
            type: 'bar',
            barWidth: 14,
            itemStyle: {
              normal: {
                color: '#5F99CB'
              }
            },
            label: {
              normal: {
                position: 'top',
                show: true
              }
            }
          }
        ]
      }

      stChart.setOption(option)
    }
  }
}
</script>

<style>
</style>
